package com.study.kafka.consumer;


import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * kafka消费者
 *
 */
public class KafkaConsumerDemo {
    public static void main(String[] args) {
        //0. 创建配置对象
        Properties props =new Properties();
        //kafka集群位置
        props.put("bootstrap.servers", "hadoop102:9092");
        //消费者组ID
        props.put("group.id", "suibian");
        //自动提交offset
        props.put("enable.auto.commit", "true");
        //提交间隔
        props.put("auto.commit.interval.ms", "1000");
        //kv的反序列化器
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

        //1. 创建consumer对象
        KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<String, String>(props);

        //2. 订阅主题
        List<String> topics =new ArrayList<String>();
        topics.add("first");
        topics.add("hello");//如果没有此主题，会自动按照默认规则：一个分区，一个副本创建
        kafkaConsumer.subscribe(topics);

        //3. 持续消费数据
        while (true){
            ConsumerRecords<String, String> records = kafkaConsumer.poll(Duration.ofSeconds(2));
            for (ConsumerRecord<String, String> record : records) {
                System.out.println("消费到："+record.topic()+
                        " : "+ record.partition()+
                        " : "+ record.offset()+
                        " : "+ record.key()+
                        " : "+ record.value()
                );
            }
        }

        //4. 关闭消费者对象
        //kafkaConsumer.close();
    }
}
